/*************************************************************
 * File: ResourceServiceImpl.java
 * Copyright (c) 2012
 * Author: Huang Shaobin
 * Email: Shaobin.Software@gmail.com Shaobin_Software@163.com 
 * Date: Feb 2, 2013
 * Description: 
 ************************************************************/

package snow.biz.security.service.impl;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import snow.biz.base.service.impl.HibernateBaseServiceImpl;
import snow.biz.security.model.Resource;
import snow.biz.security.model.Role;
import snow.biz.security.service.IResourceService;
import snow.biz.system.model.User;
import snow.core.enums.Status;
import snow.core.persistence.Pagination;
import snow.core.persistence.query.AndedConditions;
import snow.core.persistence.query.BinaryOperator;
import snow.core.persistence.query.Condition;
import snow.core.persistence.query.Conditions;
import snow.core.persistence.query.Order;
import snow.core.security.OperationType;
import snow.core.security.SecurityService;

/**
 * @author Shaobin.Software@gmail.com Shaobin_Software@163.com
 * @version 1.0
 */
public class ResourceServiceImpl extends HibernateBaseServiceImpl implements IResourceService, SecurityService {

	private final Log log = LogFactory.getLog(getClass());

	/**
	 * @param resourceUri
	 * @param operation
	 * @return
	 * @see snow.core.security.SecurityService#canAccessResource(java.lang.String, int)
	 */
	public boolean canAccessResource(String resourceUri, int operation) {
		try {
			log.debug("开始判断当前用户是否有权限访问指定资源……");
			Conditions conditions = new AndedConditions();
			conditions.addCondition(new Condition("uri", BinaryOperator.EQ, resourceUri));
			conditions.addCondition(new Condition("status", BinaryOperator.EQ, Status.SUCCESS));
			List<Resource> list = super.find(Resource.class, conditions, Arrays.asList(new Order[] { Order.desc("sort") }), -1, -1);
			final Object item = CollectionUtils.isEmpty(list) ? null : list.get(0);
			final Resource resource = item instanceof Resource ? (Resource) item : null;
			if (resource == null) {
				log.error("没有找到uri为：[" + resourceUri + "]的资源！");
				return false;
			}
			final User currentUser = super.getCurrentUser();
			final List<Role> roles = currentUser.getRoles();
			if (CollectionUtils.isNotEmpty(roles)) {
				for (final Role role : roles) {
					if (role == null) {
						continue;
					}
					if (role.hasPermission(resource, OperationType.toOperationTypeFrom(operation))) {
						return true;
					}
				}
			}
			return false;
		} finally {
			log.debug("结束判断当前用户是否有权限访问指定资源！");
		}
	}

	public Integer createResource(Resource resource) {
		return super.save(resource, Integer.class);
	}

	public void modifyResource(Resource resource) {
		super.update(resource);
	}

	public void removeResource(Resource resource) {
		super.delete(resource);
	}

	public Resource searchResourceFrom(Integer id) {
		return super.get(Resource.class, id);
	}

	public List<Resource> searchResourcesFrom(Conditions conditions, List<Order> orders) {
		return super.find(Resource.class, conditions, orders, -1, -1);
	}

	public Pagination searchResourcesFrom(Conditions conditions, List<Order> orders, int start, int limit) {
		return super.findForPaging(Resource.class, conditions, orders, start, limit);
	}

}
